package com.sky.controller.admin;

import com.github.pagehelper.PageHelper;
import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@ApiOperation("员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO); // 调用login service 服务

        Map<String , Object> claims  = new HashMap<>();
        // 设置 用户的 id
        claims.put(JwtClaimsConstant.EMP_ID , employee.getId());
        // 生成 token
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey() , jwtProperties.getAdminTtl(), claims);
        // 员工数据构造
        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO); // 返回查询该员工数据
    }

    /**
     * 退出登录
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出接口")
    public Result<String> logout() {
        return Result.success();
    }
    @PostMapping
    @ApiOperation("新增员工")
    public Result save(@RequestBody EmployeeDTO employeeDTO) {
        employeeService.save(employeeDTO);
        return Result.success();
    }
    /**
    * @Author: Zhang
    * @param:        
    * @createTime: 2025/4/23 19:25
    * @Description: TODO
    * @Return: 返回分页查询的结果
    */
    @GetMapping("/page")
    public Result<PageResult> page (EmployeePageQueryDTO employeePageQueryDTO) {
        /*
        * 1: 获取web 请求参数
        * 2: 封装分页请求参数
        * 3：调用 services 进行数据库查询
        * 4：查询 得到结果并返回前端
        * */
        log.info("分页查询参数{}", employeePageQueryDTO);
        employeeService.pageEmployeeQuery(employeePageQueryDTO);
        return  null;
    }

}
